local Lua__jiamianZH = fk.CreateSkill {
    name = "Lua__jiamianZH",
    tags = {Skill.Permanent}
}


Fk:loadTranslationTable{
  ["Lua__jiamianZH"] = "假面召唤",
  [":Lua__jiamianZH"] = "持恒技，共鸣技，出牌阶段开始时，若场上有人机，则你可以将一名人机的身份改为与你相同，且令其从3个武将牌中选一个替换其主将。若没有人机，你可以选择一名其他角色，该回合其由你控制。",
  ["#Lua__jiamianZH-use"] = "假面召唤：请选择一名人机，将其身份改成与你相同并替换主将。",
  ["#Lua__jiamianZH-use2"] = "假面召唤：请选择一名角色，其本回合由你控制。",
}




Lua__jiamianZH:addEffect(fk.EventPhaseStart, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(Lua__jiamianZH.name) and target == player and player.phase == Player.Play and
        (player.general == "DIEND" or player.deputyGeneral == "DIEND")
    end,
    on_cost =function (self, event, target, player, data)
        local enemies = {}
        for _, p in ipairs(player.room.alive_players) do
            if p.id < 0 then
              table.insert(enemies, p)
            end
        end
        if #enemies > 0 then
            local targets = player.room:askToChoosePlayers(player, {
                targets = enemies,
                min_num = 1,
                max_num = 1,
                prompt = "#Lua__jiamianZH-use",
                skill_name = Lua__jiamianZH.name,
                cancelable = true,
            })
            if #targets > 0 then
                event:setCostData(self, {target = targets[1]})
                return true
            end 
        elseif #enemies == 0 then
            local targets = player.room:askToChoosePlayers(player, {
                targets = player.room:getOtherPlayers(player),
                min_num = 1,
                max_num = 1,
                prompt = "#Lua__jiamianZH-use2",
                skill_name = Lua__jiamianZH.name,
                cancelable = true,
            })
            if #targets > 0 then
                event:setCostData(self, {target = targets[1]})
                return true
            end 
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local target = event:getCostData(self).target
        if target.id < 0 then
            room:setPlayerProperty(target, "role", player.role)
            room:notifyProperty(target, target, "role")
            room:broadcastProperty(target, "role")
            local tgen = room:askForGeneral(target, room:getNGenerals(3), 1, false)
            room:changeHero(target, tgen, false, false, true, true)
        else
            player:control(target)
        end
    end,
})

Lua__jiamianZH:addEffect(fk.TurnEnd, {
    is_delay_effect = true,
    mute = true,
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(Lua__jiamianZH.name) and target == player 
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        for _, p in ipairs(player.room.alive_players) do
            p:control(p)
        end
    end,
})


return Lua__jiamianZH 